﻿using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HalconDemoWPF0324002.Extensions;
using HalconDotNet;

namespace HalconDemoWPF0324002
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        static string modelname = "C:\\Users\\Administrator\\Desktop\\Halcon\\Assets\\0324ShapeModel.shm";
        static string filename = "C:\\Users\\Administrator\\Desktop\\Halcon\\Assets\\模板.png";
        public MainWindow()
        {
            InitializeComponent();
        }

        HImage image = new();
        HTuple modelID = new();

        List<HDrawingObjectInfo> Draws = new List<HDrawingObjectInfo>();


        public void bndrawcir(object sender, EventArgs e)
        {
            string color = "blue";


            var cir1 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, 50, 100, 50);
            cir1.SetDrawingObjectParams("color", color);
            cir1.OnDrag(CirlDrag);//当拖动
            cir1.OnResize(CirlReSize);
            cir1.OnSelect(CirlOnSelect);
            Draws.Add(new HDrawingObjectInfo()
            {
                color = color,
                HDrawObj = cir1,
                Tuples = cir1.GetTuples(),
                type = "circle"
            });

            hwin.HalconWindow.AttachDrawingObjectToWindow(cir1);
        }

        private void CirlOnSelect(HDrawingObject drawid, HWindow window, string type)
        {
            OnReflush(drawid);
        }

        private void CirlReSize(HDrawingObject drawid, HWindow window, string type)
        {
            OnReflush(drawid);
        }

        private void CirlDrag(HDrawingObject drawid, HWindow window, string type)
        {
            OnReflush(drawid);
        }

        void OnReflush(HDrawingObject id)
        {
            var x = Draws.FirstOrDefault(x => x.HDrawObj.ID == id);
            if (x != null)
            {
                x.Tuples = id.GetTuples();
            }
        }

        public void bndrawrec(object sender, EventArgs e)
        {
            string color = "red";


            var rect1 = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, 0, 0, 100, 100);
            rect1.SetDrawingObjectParams("color", color);
            rect1.OnDrag(CirlDrag);//当拖动
            rect1.OnResize(CirlReSize);
            rect1.OnSelect(CirlOnSelect);
            Draws.Add(new HDrawingObjectInfo()
            {
                color = color,
                HDrawObj = rect1,
                Tuples = rect1.GetTuples(),
                type = "rectangle1"
            });
            hwin.HalconWindow.AttachDrawingObjectToWindow(rect1);
        }



        private void bnreader(object sender, RoutedEventArgs e)
        {
            //通过HImage读取图像的方法
            image.ReadImage(filename);
            //调用Window显示图像的方法
            hwin.HalconWindow.DispImage(image);
        }
        private void bncheck(object sender, RoutedEventArgs e)
        {
            //加载模板文件
            HOperatorSet.ReadShapeModel(modelname, out modelID);

            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();

            //定位查找
            HOperatorSet.FindShapeModel(image, modelID, (new HTuple(0)).TupleRad(),
            (new HTuple(90)).TupleRad(), 0.5, 1, 0.5, "least_squares", 0, 0.9, out hv_Row,
            out hv_Column, out hv_Angle, out hv_Score);

            //设置窗口画线颜色
            HOperatorSet.SetColor(hwin.HalconWindow, "red");
            //画出十字
            HOperatorSet.DispCross(hwin.HalconWindow, hv_Row.DArr[0], hv_Column.DArr[0], 30, 0);

            var msg = $"匹配分数：{hv_Score.DArr[0]} Row：{hv_Row.DArr[0]} Column：{hv_Column.DArr[0]}";

            this.txt.Text = msg;
            Console.WriteLine(msg);
        }

        private void bngenrect(object sender, RoutedEventArgs e)
        {
            var reg1 = Draws.FirstOrDefault(x => x.type.Contains("rectangle1"));
            var region = reg1.GenObject();
            //截取指定区域范围内图像
            HOperatorSet.ReduceDomain(image, region, out HObject iamgereduce);
            //取出实际
            HOperatorSet.CropDomain(iamgereduce, out HObject imagepart);
            this.hwin.HalconWindow.DispObj(imagepart);


            ///创建模板
            HOperatorSet.CreateShapeModel(imagepart, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out HTuple ModelID);
            ///保存模板文件
            HOperatorSet.WriteShapeModel(ModelID, modelname); 
        }

        private void bngencircl(object sender, RoutedEventArgs e)
        {

        }
    }
}